package com.qf.agro.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.qf.agro.pojo.SysRole;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author 千锋健哥
 */
@Repository
public interface RoleMapper extends BaseMapper<SysRole> {

    /**
     * 根据用户id获取角色列表
     * @param userId
     * @return
     */
    @Select("select c.* " +
            "from sa_sys_user a  " +
            "left join sa_sys_user_role b on a.id = b.user_id " +
            "left join sa_sys_role c on c.id = b.role_id " +
            "where a.id=#{id}")
    public List<SysRole> findListByUserId(@Param("id") Long userId);

    /**
     * 根据用户id, 删除对应角色的关联表数据
     * @param userId
     */
    @Delete("delete from sa_sys_user_role where user_id=#{id}")
    public void deleteByUserId(@Param("id") Long userId);

    /**
     * 向用户和角色关联表中添加关联关系
     * @param userId
     * @param roleId
     */
    @Insert("insert into sa_sys_user_role(user_id, role_id) values(#{userId}, #{roleId})")
    public void insertUserIdAndRoleId(@Param("userId") Long userId,
                                      @Param("roleId") Long roleId);
}
